<?php
namespace Admin\Controller;
//use Think\Controller;
use Tools\AdminController;
//角色控制
class AuthController extends AdminController {

    function zhanshi(){
        //访问过滤

        //获取所有权限，排序，展示
        $auth = D("Auth");

        $infoall = $auth->select();
        if(!empty($_GET)&&$_GET['auth_id']!=0){
            $info = $auth->select($_GET['auth_id']);

        }else{
            $info = $auth->order('auth_path')->select();
        }
//        dump($info);

        $this->assign('infoall',$infoall);
        $this->assign("info",$info);
        $this->display();
    }
    function tianjia(){
        $auth = new \Model\AuthModel();
        //两个逻辑 展示 收集
        if(!empty($_POST)){
            //四个信息 name pid controller action
            //根据已有四个字段制作path level 并实现记录存入数据库
            $z = $auth->saveData($_POST);
            if($z){
                $this->redirect('zhanshi',array(),1,'添加权限成功');
            }else{
                $this->redirect('zhanshi',array(),1,'添加权限失败');
            }
        }else{
            //获得顶级权限信息
            $auth_infoA = $auth->where('auth_level=0')->select();

            $this->assign("auth_infoA",$auth_infoA);
            $this->display();
        }
    }
    public function xiugai($auth_id){
        //$goods_id,展示表传递过来的

        $auth = new \Model\AuthModel();
        //两个逻辑：展示，收集
        if(!empty($_POST)){
            //收集表单，并存入数据库
            $z = $auth->save($_POST);
            if($z){
//                $this->redirect([分组/控制器/操作方法]地址，参数，延迟时间，提示信息);
                $this->redirect('zhanshi',array(),1,'修改权限成功');
            }else{
                $this->redirect('xiugai',array('goods_id'=>$auth_id),1,'修改权限失败');
            }
        }else{
            //根据$goods_id获得被修改的商品信息，并给模板
            //find()方法只负责给返回一条记录结果，并且是一维数组返回
            $info = $auth->find($auth_id);
            $pid = $info['auth_pid'];

            $auth_infoA = $auth->where('auth_level=0')->select();
            $auth_infoB = $auth->find($pid);
//            dump($auth_infoB);
            $this->assign("auth_infoA",$auth_infoA);
            $this->assign("auth_infoB",$auth_infoB);
            //展示表单
//            dump($auth_infoA);
            $this ->assign('info',$info);
            $this->display();
        }
    }
    //删除
    function shanchu($auth_id){
        $auth = D('Auth');
        $z = $auth->delete($auth_id);
        if($z){
            $this->redirect('zhanshi',array(),1,'删除权限成功');
        }else{
            $this->redirect('zhanshi',array('role_id'=>$auth_id),1,'删除权限失败');
        }
    }
}